package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 列表查询
     */
    @Override
    public PageResult<Business> businessList(BusinessDto businessDto) {
        Page<Business> page =businessMapper.businessList( new Page<Business>(businessDto.getPage(),businessDto.getPageSize()), businessDto);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    /**
     * 根据id查询商机，包含跟进列表
     */
    @Override
    public Business getBusinessById(Integer id) {
        return businessMapper.getBusinessById(id);
    }
    /**
     * 跟进商机
     */
    @Transactional(rollbackFor = Exception.class)//事务控制
    @Override
    public void trackRcord(Business business) {
        //1.基础信息的修改
        business.setStatus(3);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);
        //2.跟进信息的插入
        BusinessTrackRecord businessTrackRecord =new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//获取当前登录用户的ID
        businessTrackRecord.setTrackStatus(business.getTrackStatus());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    /**
     * 踢回公海
     */
    @Override
    public void back(Integer id) {
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);
    }
    /**
     * 商机转客户
     */
    @Override
    public void toCustomer(Integer id) {
        //1.更新商机基础信息
        Business business = businessMapper.selectById(id);
        business.setId(id);
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);
        //2.拷贝线索信息到商机列表
        Customer customer =new Customer();
        customer.setPhone(business.getPhone());
        customer.setChannel(business.getChannel());
        customer.setName(business.getName());
        customer.setGender(business.getGender());
        customer.setAge(business.getAge());
        customer.setWechat(business.getWechat());
        customer.setQq(business.getQq());
        customer.setDegree(business.getDegree());
        customer.setJobStatus(business.getJobStatus());
        customer.setSubject(business.getSubject());
        customer.setCourseId(business.getCourseId());
        customer.setBusinessId(business.getId());
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> poolList(BusinessDto businessDto) {
        Page<Business> page =businessMapper.poolList( new Page<Business>(businessDto.getPage(),businessDto.getPageSize()), businessDto);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }
}
